package stackdemo;

import java.util.LinkedList;
import java.util.Stack;

/**
 * @Author 12629
 * @Description：
 */
public class Test {

    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0;i < pushV.length;i++) {
            stack.push(pushV[i]);

            while(j < popV.length && !stack.empty()
                    && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }

        }
        return stack.empty();
    }

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i < tokens.length;i++) {
            String tmp = tokens[i];
            if(!isOperations(tmp)) {
                stack.push(Integer.parseInt(tmp));
            }else {
                Integer val2 = stack.pop();
                Integer val1 = stack.pop();
                switch(tmp) {
                    case "+":
                        stack.push(val1+val2);
                        break;
                    case "-":
                        stack.push(val1-val2);
                        break;
                    case "*":
                        stack.push(val1*val2);
                        break;
                    case "/":
                        stack.push(val1/val2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    private boolean isOperations(String tmp) {
        if(tmp.equals("+") || tmp.equals("-")
                ||tmp.equals("*") ||tmp.equals("/")) {
            return true;
        }
        return false;
    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i < s.length();i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                if(stack.empty()) {
                    return false;
                }
                char topL = stack.peek();
                if(topL == '(' && ch == ')' || topL == '[' && ch == ']'
                        || topL == '{' && ch == '}') {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(!stack.empty()) {
            return false;
        }

        return true;
    }
    public static void main(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println(stack.pop());
        System.out.println(stack.peek());
        System.out.println(stack.peek());
    }
    public static void main2(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);

        System.out.println(myStack.pop());
        System.out.println(myStack.peek());
        System.out.println(myStack.peek());
    }
    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);

        System.out.println(stack.pop());
        //获取栈顶元素 不删除
        System.out.println(stack.peek());
        System.out.println(stack.peek());
    }
}
